package com.lxd.mq;

import com.msb.framework.redis.RedisClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.security.Key;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * @Author Dongdong
 * @注释
 */
@RestController
@Slf4j
public class cacheController {

    @Autowired
    private RedisClient redisClient;

    @PostMapping(value = "/cache/set/{key}")
    public void set(@PathVariable(value = "key") String key, @RequestParam(value = "value") Object value) {
        log.info("【缓存模块】 set方法存储 key={},value={}", key, value);
        redisClient.set(key, value);
        redisClient.expire(key,6, TimeUnit.DAYS);//设置key的存在时间为6h
    }
    @PostMapping(value = "/cache/setnNx/{key}/{value}/{ttl}")
    public boolean setNx(@PathVariable(value = "key") String key,
                      @RequestParam(value = "value") Object value,
                      @PathVariable(value = "ttl") Long ttl ){
        return redisClient.setNx(key,value,ttl,TimeUnit.SECONDS);
    }

    @PostMapping(value = "/cache/setlong/{key}")
    public void setLong(@PathVariable(value = "key") String key, @RequestParam(value = "value") Long value) {
        log.info("【缓存模块】 set方法存储 key={},value={}", key, value);
        redisClient.set(key, value);

        redisClient.expire(key,6, TimeUnit.DAYS);//设置key的存在时间为6h
    }

    @PostMapping(value = "/cache/hmset/{key}")
    public void hSet(@PathVariable(value = "key") String key, @RequestBody Map<String, Object> value) {
        log.info("【缓存模块】 hSet方法存储 key={},value={}", key, value);
        redisClient.hSet(key, value);

        redisClient.expire(key,6, TimeUnit.DAYS);//设置key的存在时间为6h
    }


    // List<Map<>> ==Map<String,Object>...
    @PostMapping(value = "/cache/sadd/{key}")
    public void sadd(@PathVariable(value = "key")String key, @RequestBody Map<String,Object>... value){
        log.info("【缓存模块】 sadd方法，存储key = {}，存储value = {}", key, value);
        redisClient.sAdd(key,value);
        redisClient.expire(key,6, TimeUnit.DAYS);//设置key的存在时间为6h
    }

    @GetMapping("/cache/hgetall/{key}")
    public Map hGetAll(@PathVariable(value = "key")String key){
        log.info("【缓存模块】 hGetAll方法，要查询的key = {}", key);
        Map<String, Object> value = redisClient.hGetAll(key);
        log.info("【缓存模块】 hGetAll方法，存储value = {}", value);
//        redisClient.expire(key,6, TimeUnit.HOURS);//设置key的存在时间为6h
        return value;
    }

    @GetMapping("/cache/hget/{key}/{field}")
    public Object hget(@PathVariable(value = "key")String key,@PathVariable(value = "field")String field){
        log.info("【缓存模块】 hget方法，获取key ={}，field = {}的数据", key,field);
        Object value = redisClient.hGet(key, field);
        log.info("【缓存模块】 hGet方法，获取key ={}，field = {} 的数据 value = {}", key,field,value);
        return value;
    }

    @GetMapping("/cache/smembers/{key}")
    public Set smembers(@PathVariable(value = "key") String key){
        log.info("【缓存模块】 getSet方法，获取key ={}的数据", key);
        Set<Map> value = redisClient.sMembers(key);
        log.info("【缓存模块】 getSet方法，获取key ={}，field = {} 的数据 value = {}", key,value);
        return value;
    }

    @GetMapping("/cache/get/{key}")
    public Object get(@PathVariable(value = "key") String key){
        log.info("【缓存模块】 get方法，获取key ={}的数据", key);
        Object value = redisClient.get(key);
        log.info("【缓存模块】 getSet方法，获取key ={}，field = {} 的数据 value = {}", key,value);
        return value;
    }

    @PostMapping("/cache/pipeline/string")
    public void pipeline(@RequestBody Map<String,String> map){
        log.info("【缓存模块】 pipelineString，获取到存储的数据，map的长度 ={}的数据", map.size());
        // map的key 为 手机号   value 为 区域+运营商
        redisClient.pipelined(operations->{
            for (Map.Entry<String, String> entry : map.entrySet()) {
                operations.opsForValue().set(entry.getKey(),entry.getValue());
            }
        });
    }
    // 添加String 到set 结构
    @PostMapping(value = "/cache/saddstr/{key}")
    public void saddStr(@PathVariable(value = "key")String key, @RequestBody String... value){
        log.info("【缓存模块】 saddStr方法，存储key = {}，存储value = {}", key, value);
        redisClient.sAdd(key,value);
        redisClient.expire(key,6, TimeUnit.DAYS);//设置key的存在时间为6h

    }

    @PostMapping(value = "/cache/zadd/{key}/{score}/{member}")
    public Boolean zadd(@PathVariable(value = "key")String key,
                        @PathVariable(value = "score")Long score,
                        @PathVariable(value = "member")Object member){
        log.info("【缓存模块】 zaddLong方法，存储key = {}，存储score = {}，存储value = {}", key,score, member);
        Boolean result = redisClient.zAdd(key, member, score);
        return result;
    }

    @GetMapping(value = "/cache/zrangebyscorecount/{key}/{start}/{end}")
    public int zRangeByScoreCount(@PathVariable(value = "key") String key,
                                  @PathVariable(value = "start") Double start,
                                  @PathVariable(value = "end") Double end) {
        log.info("【缓存模块】 zRangeByScoreCount方法，查询key = {},start = {},end = {}", key,start,end);


        Set<Object> values = redisClient.zRangeByScore(key, start, end);
        if(values != null){
            //返回数据大小 （短信数量）
            return values.size();
        }
        return 0;
    }

    @DeleteMapping(value = "/cache/zremove/{key}/{member}")
    public void zRemove(@PathVariable(value = "key") String key,@PathVariable(value = "member") String member) {
        log.info("【缓存模块】 zRemove方法，删除key = {},member = {}", key,member);
        redisClient.zRemove(key,member);
    }

    @PostMapping(value = "/cache/hincrby/{key}/{field}/{delta}")
    public Long hIncrBy(@PathVariable(value = "key") String key,
                        @PathVariable(value = "field") String field,
                        @PathVariable(value = "delta") Long delta){
        log.info("【缓存模块】 hIncrBy方法，自增   key = {},field = {}，number = {}", key,field,delta);

        Long result = redisClient.hIncrementBy(key, field, delta);
        log.info("【缓存模块】 hIncrBy方法，自增   key = {},field = {}，number = {},剩余数值为 = {}", key,field,delta,result);
        return result;
    }
    @PostMapping(value = "/cache/decr/{key}/{delta}")
    public Long decr(@PathVariable(value = "key") String key,
                        @PathVariable(value = "delta") Long delta){
        log.info("【缓存模块】 decrement方法，自减  key = {},number = {}", key,delta);
        Long result = redisClient.decrement(key, delta);
        log.info("【缓存模块】 decrement方法，自减   key = {}，number = {},剩余数值为 = {}", key,delta,result);
        return result;
    }

    @PostMapping(value = "/cache/incr/{key}/{delta}")
    public Long Incr(@PathVariable(value = "key") String key,
                     @PathVariable(value = "delta") Long delta){
        log.info("【缓存模块】 increment方法，自增  key = {},number = {}", key,delta);
        Long result = redisClient.increment(key, delta);
        log.info("【缓存模块】 increment方法，自增   key = {}，number = {},剩余数值为 = {}", key,delta,result);
        return result;
    }

    @PostMapping(value = "/cache/keys/{pattern}")
    public Set<String> keys(@PathVariable String pattern){
        log.info("【缓存模块】 keys方法，根据pattern查询key的信息  pattern = {}" ,pattern);
        Set<String> keys = redisClient.hKeys(pattern);
        log.info("【缓存模块】 keys方法，根据pattern查询key的信息  pattern = {},查询出全部的key信息 keys = {}" ,pattern,keys);
        return keys;
    }



}
